package com.hooper.executor.demo;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/03/02/21:52
 */
public class TestShutdown {

    @Test
    public void test() {
        //空参创建
        //单线程执行
        ExecutorService es = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            //全部执行成功
            es.submit(new MyTask());
        }
        //关闭线程池，但不会停止当前线程池中已经有的任务
        es.shutdown();
        //不能执行新的任务
        es.submit(() -> System.out.println("new task"));
    }

    @Test
    public void test2() {
        ExecutorService es = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            es.submit(new MyTask());
        }
        //立刻关闭线程池，并关闭所有线程，返回被关闭的线程集合
        List<Runnable> list = es.shutdownNow();
        list.forEach(System.out::println);
    }
}
